package com.CST.service.impl.ys;

import com.CST.dto.ys.YsDetailDTO;
import com.CST.util.DateStringUtil;
import com.CST.util.ExcelTransfer;
import com.CST.vo.ys.AssetDetailVO;
import com.CST.vo.ys.YsIndexDetailsVO;
import com.CST.entity.ys.YsIndexDetail;
import com.CST.mapper.cst.ys.YsIndexDetailMapper;
import com.CST.service.ys.YsIndexDetailService;
import com.CST.util.PageHelperTool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qtx
 * @since 2023-04-21
 */
@Service
public class YsIndexDetailServiceImpl extends ServiceImpl<YsIndexDetailMapper, YsIndexDetail> implements YsIndexDetailService {

    private final YsIndexDetailMapper dao;

    @Autowired
    private ExcelTransfer<YsIndexDetailsVO> excelTransfer;

    public YsIndexDetailServiceImpl(YsIndexDetailMapper dao) {
        this.dao = dao;
    }

    @Override
    public PageInfo<YsIndexDetailsVO> listIndexInfo(YsDetailDTO dto) {
        Optional.ofNullable(dto.getStartTime())
                .orElseThrow(()->new RuntimeException("起始时间不能为空"));
        Optional.ofNullable(dto.getEndTime())
                .orElseThrow(()->new RuntimeException("截止时间不能为空"));
        Optional.ofNullable(dto.getNum())
                .orElseThrow(()->new RuntimeException("页码不能为空"));
        Optional.ofNullable(dto.getSize())
                .orElseThrow(()->new RuntimeException("页大小不能为空"));
        if(!dto.getStartTime().split("-")[0].equals(dto.getEndTime().split("-")[0])){
            throw  new RuntimeException("年份不能跨越");
        }
        dto.setStart(DateStringUtil.transformer(dto.getStartTime()));
        dto.setEnd(DateStringUtil.transformer(dto.getEndTime()));
        List<YsIndexDetailsVO> detailsVOS = dao.listIndexInfo(dto);
        return PageHelperTool.
                excutePageInfo(detailsVOS, dto.getNum(), dto.getSize());
    }

    @Override
    public YsIndexDetailsVO getIndexById(Integer num) {
        Optional.ofNullable(num)
                .orElseThrow(()->new RuntimeException("编码不能为空"));
        return dao.getIndexById(num);
    }

    @Override
    public String exportIndexList(YsDetailDTO dto, HttpServletResponse response) {
        Optional.ofNullable(dto.getStartTime())
                .orElseThrow(()->new RuntimeException("起始时间不能为空"));
        Optional.ofNullable(dto.getEndTime())
                .orElseThrow(()->new RuntimeException("截止时间不能为空"));
        if(!dto.getStartTime().split("-")[0].equals(dto.getEndTime().split("-")[0])){
            throw  new RuntimeException("年份不能跨越");
        }
        try {
            dto.setStart(DateStringUtil.transformer(dto.getStartTime()));
            dto.setEnd(DateStringUtil.transformer(dto.getEndTime()));
            excelTransfer.exportExcel(response,dao.listIndexInfo(dto),"业务量配套指标","sheet",YsIndexDetailsVO.class);
            return "导出成功";
        }catch (Exception e){
            throw new RuntimeException("导出失败");
        }
    }
}
